/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Bean.SchedulingInterviewBean;
import Bean.VacancyOfApplicantBean;
import Utility.DBProcess;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Vietlt
 */
public class VacancyOfApplicantDAO {
    public static void main(String[] args) {
        VacancyOfApplicantBean va=new VacancyOfApplicantBean();
        va.setVacancyID(1);
        va.setApplicantID(3);
        List<VacancyOfApplicantBean> list=new VacancyOfApplicantDAO().LoadAllVacancyOfApplicant();
        for (VacancyOfApplicantBean applicantBean : list) {
        System.out.println("ssss"+":"+applicantBean.getApplicantID());
        System.out.println("ssss"+":"+applicantBean.getVacancyID());
            System.out.println("------------");
        }
    }
    public List<VacancyOfApplicantBean> LoadAllVacancyOfApplicant() {
        List<VacancyOfApplicantBean> list = new ArrayList<VacancyOfApplicantBean>();
        Connection conn = null;
        CallableStatement stm = null;
        ResultSet rs = null;
        conn = DBProcess.getConnection();
        try {
            stm = conn.prepareCall("{ call VacancyOfApplicant_getall}");
            rs = stm.executeQuery();
            while (rs.next()) {
                VacancyOfApplicantBean Va = new VacancyOfApplicantBean();
                Va.setVacancyID(rs.getInt("VacancyID"));
                Va.setApplicantID(rs.getInt("ApplicantID"));
                list.add(Va);
                if (list.isEmpty()) {
                    String err = "List empty !!!";
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(ApplicantDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DBProcess.closeResultSet(rs);
            DBProcess.closeStatement(stm);
            DBProcess.closeConnection(conn);
        }
        return list;
    }

    public VacancyOfApplicantBean LoadAllVacancyOfApplicantById(int VacancyID) {
        Connection conn = null;
        CallableStatement stm = null;
        ResultSet rs = null;
        conn = DBProcess.getConnection();
        VacancyOfApplicantBean Va = new VacancyOfApplicantBean();
        try {
            stm = conn.prepareCall("{ call VacancyOfApplicant_get (?)}");
            stm.setInt(1, VacancyID);
            rs = stm.executeQuery();
            while (rs.next()) {
                Va.setVacancyID(rs.getInt("VacancyID"));
                Va.setApplicantID(rs.getInt("ApplicantID"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ApplicantDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DBProcess.closeResultSet(rs);
            DBProcess.closeStatement(stm);
            DBProcess.closeConnection(conn);
        }
        return Va;
    }

    public int AddNewVacancyOfApplicant(VacancyOfApplicantBean va) {
        Connection conn = null;
        CallableStatement stm = null;
        conn = DBProcess.getConnection();
        int isComplete = 0;

        try {
             stm = conn.prepareCall("{ call VacancyOfApplicant_add(?,?)}");
            stm.setInt(1, va.getVacancyID());
            stm.setInt(2, va.getApplicantID());
            if (stm.executeUpdate() > 0) {
                isComplete = 1;
            } else {
                isComplete = 0;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return isComplete;
    }
    public int UpdateVacancyOfApplicant(VacancyOfApplicantBean sc) {
        Connection conn = null;
        CallableStatement stm = null;
        conn = DBProcess.getConnection();
        int isComplete = 0;

        try {
            stm = conn.prepareCall("{ call VacancyOfApplicant_update(?,?)}");
            stm.setInt(1, sc.getVacancyID());
            stm.setInt(2, sc.getApplicantID());
            if (stm.executeUpdate() > 0) {
                isComplete = 1;
            } else {
                isComplete = 0;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return isComplete;
    }
    public int DeleteVacancyOfApplicant(int VacancyID) {
        Connection conn = null;
        CallableStatement stm = null;
        conn = DBProcess.getConnection();
        int isComplete = 0;

        try {
            stm = conn.prepareCall("{ call VacancyOfApplicant_delete(?)}");
            stm.setInt(1, VacancyID);
            if (stm.executeUpdate() > 0) {
                isComplete = 1;
            } else {
                isComplete = 0;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return isComplete;
    }
    
}
